#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Author : t_zhehang
# Data : 17-3-24
# 小易来到了一条石板路前，每块石板上从1挨着编号为：1、2、3.......
# 这条石板路要根据特殊的规则才能前进：对于小易当前所在的编号为K的 石板，小易单次只能往前跳K的一个约数(不含1和K)步，即跳到K+X(X为K的一个非1和本身的约数)的位置。 小易当前处在编号为N的石板，他想跳到编号恰好为M的石板去，小易想知道最少需要跳跃几次可以到达。
# 例如：
# N = 4，M = 24：
# 4->6->8->12->18->24
# 于是小易最少需要跳跃5次，就可以从4号石板跳到24号石板
# 输入描述:
# 输入为一行，有两个整数N，M，以空格隔开。
# (4 ≤ N ≤ 100000)
# (N ≤ M ≤ 100000)
#
#
# 输出描述:
# 输出小易最少需要跳跃的步数,如果不能到达输出-1
#
# 输入例子:
# 4 24
#
# 输出例子:
# 5


def getMoves(n):

    moves = []
    for i in range(2, n):
        if i * i > n:
            break;
        if n % i == 0:
            moves.append(i)
            if i * i != n:
                moves.append(n / i)
        else:
            i += 1
    moves.sort()
    return moves

def solveDP(n ,m):

    result = [-1 for i in range(m+1)]

    result[n] = 0
    for i in range(n, m+1):
        if result[i] == -1:
            continue
        else:
            moves = getMoves(i)
            for move in moves:
                if move + i > m:
                    break
                if move + i == m:
                    return result[i] + 1

                elif result[i+move] == -1 or result[i+move] > result[i] + 1:

                        result[i+move] = result[i] + 1

    return result[m]


if __name__ == "__main__":

    temp = raw_input().split(" ")

    n = int(temp[0])
    m = int(temp[1])

    print solveDP(n, m)

# import math
#
#
# def getMoves(n):
#
#     moves = []
#     for i in range(2, n):
#         if i * i > n:
#             break;
#         if n % i == 0:
#             moves.append(i)
#             if i * i != n:
#                 moves.append(n / i)
#         else:
#             i += 1
#     moves.sort()
#     return moves
#
#
# def solveDP(n ,m):
#
#     result = [-1 for i in range(m+1)]
#
#     result[n] = 0
#     result_temp = [n]
#     result_temp2 = []
#
#
#     while len(result_temp) != 0:
#         for i in result_temp:
#             moves = getMoves(i)
#             for move in moves:
#                 if move + i > m:
#                     break
#                 elif move + i == m:
#                     result[i + move] = result[i] + 1
#                     return result[i + move]
#                 elif result[i + move] == -1 or result[i + move] > result[i] + 1:
#                     result[i + move] = result[i] + 1
#                     result_temp2.append(i + move)
#         result_temp = result_temp2
#         result_temp2 = []
#
#     return result[m]
#
#
# if __name__ == "__main__":
#
#     temp = raw_input().split(" ")
#
#     n = int(temp[0])
#     m = int(temp[1])
#
#     print solveDP(n, m)











